package com.xujh;

public class FieldNameConverter {
    /**
     * 将输入的字段名转换为大写加下划线的形式（蛇形命名法）
     * 例如：userName -> USER_NAME
     */
    public static String convertToSnakeCaseUpper(String fieldName) {
        if (fieldName == null || fieldName.isEmpty()) {
            return fieldName;
        }
        
        // 处理驼峰命名法
        StringBuilder snakeCase = new StringBuilder();
        for (char c : fieldName.toCharArray()) {
            if (Character.isUpperCase(c)) {
                if (snakeCase.length() > 0) {
                    snakeCase.append('_');
                }
                snakeCase.append(c);
            } else {
                snakeCase.append(Character.toUpperCase(c));
            }
        }
        
        // 处理其他分隔符（如空格、连字符等）
        String result = snakeCase.toString()
                .replace(' ', '_')
                .replace('-', '_');
        
        // 处理多个连续下划线的情况
        return result.replaceAll("_+", "_");
    }

    public static void main(String[] args) {
        String[] fieldNames = {"MarketIndicator","PartyInfoType"};
        for (String name : fieldNames) {
            System.out.println(name + " -> " + convertToSnakeCaseUpper(name));
        }
    }
}    